<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/27 0027
 * Time: 17:10
 */
//文件上传
function upd_file($file,$dir){

    //判断$file
    if(empty($file['tmp_name'])||$file['error']!=0){
        return false;
    }

    //判断文件夹是否存在
    if(!is_dir($dir)){
        mkdirs($_SERVER['DOCUMENT_ROOT'].$dir); //创建文件夹
    }
    //开始上传
    //1.得到后缀
    $hz=explode('.',$file['name']);
    $hz=$hz[count($hz)-1];
    $path=$dir.'/'.time().rand().'.'.$hz; //得到文件最终路径

//    $_SERVER['DOCUMENT_ROOT'] :网站根目录
    if(move_uploaded_file($file['tmp_name'],$_SERVER['DOCUMENT_ROOT'].$path)){
//        $pathx=getPic($path);
        return $path; //返回给调用处
    }else{
        return false;
    }
}

//二级分类排序
//$arr 要排序的类别
//$pid 父类   无限级分类排序加形参$lav=1
function fl($arr,$pid=0){
    static $list;
    foreach($arr as $k=>$v){
        if($v['pid']==$pid){
            $list[]=$v;
            unset($arr[$k]);
            fl($arr,$v['id']);//fl($arr,$v['id'],$lav++)
        }
    }
    return $list;
}

//无限级创建目录
//$dir 传入的路径
function mkdirs($dir)
{
    if(!is_dir($dir)){
        if(!mkdirs(dirname($dir))){
            return false;}
        if(!mkdir($dir,0777)){
            return false;}
    }
    return true;
}

//输出点击排行的数据
function djph($sql){

    $djph=db_sel($sql,2);

    $msg= "<h3 class=\"ph\">
                <p>点击<span>排行</span></p>
            </h3><ul class=\"paih\">";
    foreach($djph as $v){
        $msg.="<li><a href='./new.php?id= {$v['id']}' title='{$v['title']}' target='_blank'>{$v['title']}</a></li> ";
    }
    $msg.='</ul>';

    return $msg;
}

//输出最新文章数据

function zxwz($sql){

    $data=db_sel($sql,2);

    $msg= "<h3 class=\"ph\">
                <p>最新<span>文章</span></p>
            </h3><ul class=\"paih\">";
    foreach($data as $v){
        $msg.="<li><a href='./new.php?id= {$v['id']}' title='{$v['title']}' target='_blank'>{$v['title']}</a></li> ";
    }
    $msg.='</ul>';

    return $msg;
}

/**
 * 生成缩略图
 *由显示文章引入
 * @param str $oimg
 * @param int $sw 生成缩略图的宽
 * @param int $sh 生成缩略图的高
 * @return str 生成缩略图的路径
 */
function getPic($oimg , $sw=710 , $sh = 300){
    //缩略图存放的路径的名称
    $simg = dirname($oimg) . '/'  .time().rand() . '.png';

    //获取大图和缩略图的绝对路径
    $opath = $_SERVER['DOCUMENT_ROOT'].$oimg;//原图的绝对路径
    $spath = $_SERVER['DOCUMENT_ROOT'].$simg;//最终生成的小图

    //创建小画布
    $spic = imagecreatetruecolor($sw, $sh);

    //创建白色
    $white = imagecolorallocate($spic, 255, 255, 255);
    imagefill($spic, 0, 0, $white);

    //获取大图信息
    list($bw , $bh ,$btype) = getimagesize($opath);
    //1 = GIF，2 = JPG，3 = PNG，4 = SWF，5 = PSD，6 = BMP，
    //7 = TIFF(intel byte order)，8 = TIFF(motorola byte order)，9 = JPC，10 = JP2，
    //11 = JPX，12 = JB2，13 = SWC，14 = IFF，15 = WBMP，16 = XBM
    $map = array(
        1=>'imagecreatefromgif',
        2=>'imagecreatefromjpeg',
        3=>'imagecreatefrompng',
        15=>'imagecreatefromwbmp'
    );
    if(!isset($map[$btype])) {
        return false;
    }
    $opic = $map[$btype]($opath);//大图资源
    //imagecreatefromjpeg(filename)

    if($bw>$sw||$bh>$sh){

        //计算缩略比
        $rate = min($sw/$bw , $sh/$bh);
        $zw = $bw * $rate;//最终返回的小图宽
        $zh = $bh * $rate;//最终返回的缩略小图高

        imagecopyresampled($spic, $opic, ($sw-$zw)/2, ($sh-$zh)/2, 0, 0, $zw, $zh, $bw, $bh);
        $bg2=imagecolorallocate($spic,0,0,0);
        imagettftext($spic,20,mt_rand(-40,40),500,200,$bg2,'../public/home/qingshu.otf','临心博客');
        imagepng($spic , $spath);
    }else{
        $bg2=imagecolorallocate($opic,0,0,0);
        imagettftext($opic,20,mt_rand(-40,40),$bw-100,$bh-100,$bg2,'../public/home/qingshu.otf','临心博客');
        imagepng($opic , $spath);
    }


    imagedestroy($spic);
    imagedestroy($opic);

    return $simg;

}